/**
 * 
 */
package com.gfwy.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.gfwy.dao.ILinkDao;
import com.gfwy.vo.Link;

/**
 * @author Administrator
 *
 */
public class LinkDaoImpl implements ILinkDao {
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	public LinkDaoImpl(Connection conn) {
		this.conn = conn;
	}
	/* (non-Javadoc)
	 * @see com.hospital.dao.ILinkDao#doCreate(com.hospital.vo.Link)
	 */
	@Override
	public boolean doCreate(Link link) throws Exception {
		boolean flag = false;
		try {
			String sql = "INSERT INTO link(lname,link) VALUES(?,?)";
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setString(1, link.getLname());
			this.pstmt.setString(2, link.getLink());
			if(this.pstmt.executeUpdate() > 0) {
				flag = true;
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if(this.pstmt != null) {
				try {
					this.pstmt.close();
				} catch (Exception e) {
					throw e;
				}
			}
		}
		return flag;
	}

	/* (non-Javadoc)
	 * @see com.hospital.dao.ILinkDao#doUpdate(com.hospital.vo.Link)
	 */
	@Override
	public boolean doUpdate(Link link) throws Exception {
		boolean flag = false;
		try {
			String sql = "UPDATE link SET lname=?,link=? WHERE lid=?";
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setString(1, link.getLname());
			this.pstmt.setString(2, link.getLink());
			this.pstmt.setInt(3, link.getLid());
			if(this.pstmt.executeUpdate() > 0) {
				flag = true;
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if(this.pstmt != null) {
				try {
					this.pstmt.close();
				} catch (Exception e) {
					throw e;
				}
			}
		}
		return flag;
	}

	/* (non-Javadoc)
	 * @see com.hospital.dao.ILinkDao#doDelete(int)
	 */
	@Override
	public boolean doDelete(int lid) throws Exception {
		boolean flag = false;
		try {
			String sql = "DELETE FROM link WHERE lid=?";
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setInt(1, lid);
			if(this.pstmt.executeUpdate() > 0) {
				flag = true;
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if(this.pstmt != null) {
				try {
					this.pstmt.close();
				} catch (Exception e) {
					throw e;
				}
			}
		}
		return flag;
	}

	/* (non-Javadoc)
	 * @see com.hospital.dao.ILinkDao#findAll(java.lang.String, int, int)
	 */
	@Override
	public List<Link> findAll(String keyWord, int startIndex, int count)
			throws Exception {
		List<Link> list = null;
		keyWord = (keyWord == null) ? "" : keyWord;
		try {
			String sql = "SELECT * FROM link WHERE lname LIKE ? ORDER BY ord ASC LIMIT ?,?";
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setString(1, "%" + keyWord + "%");
			this.pstmt.setInt(2, startIndex);
			this.pstmt.setInt(3, count);
			this.rs = this.pstmt.executeQuery();
			list = new ArrayList<Link>();
			while(this.rs.next()) {
				Link link = new Link();
				link.setLid(this.rs.getInt("lid"));
				link.setLname(this.rs.getString("lname"));
				link.setLink(this.rs.getString("link"));
				link.setOrd(this.rs.getInt("ord"));
				list.add(link);
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if(this.rs != null) {
				try {
					this.rs.close();
				} catch (Exception e) {
					throw e;
				}
			}
			if(this.pstmt != null) {
				try {
					this.pstmt.close();
				} catch (Exception e) {
					throw e;
				}
			}
		}
		return list;
	}

	/* (non-Javadoc)
	 * @see com.hospital.dao.ILinkDao#findById(int)
	 */
	@Override
	public Link findById(int lid) throws Exception {
		Link link = null;
		try {
			String sql = "SELECT * FROM link WHERE lid=?";
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setInt(1, lid);
			this.rs = this.pstmt.executeQuery();
			if(this.rs.next()) {
				link = new Link();
				link.setLid(this.rs.getInt("lid"));
				link.setLname(this.rs.getString("lname"));
				link.setLink(this.rs.getString("link"));
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if(this.rs != null) {
				try {
					this.rs.close();
				} catch (Exception e) {
					throw e;
				}
			}
			if(this.pstmt != null) {
				try {
					this.pstmt.close();
				} catch (Exception e) {
					throw e;
				}
			}
		}
		return link;
	}

	@Override
	public int count(String keyWord) throws Exception {
		int count = 0;
		keyWord = (keyWord == null) ? "" : keyWord;
		try {
			String sql = "SELECT COUNT(*) AS count FROM link WHERE lname LIKE ?";
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setString(1, "%" + keyWord + "%");
			this.rs = this.pstmt.executeQuery();
			if(this.rs.next()) {
				count = this.rs.getInt("count");
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if(this.rs != null) {
				try {
					rs.close();
				} catch (Exception e) {
					throw e;
				}
			}
			if(this.pstmt != null) {
				try {
					pstmt.close();
				} catch (Exception e) {
					throw e;
				}
			}
		}
		return count;
	}

}
